package com.findviewbydream.evil.eda.Activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.findviewbydream.evil.eda.Bean.FlowCallbackBean;
import com.findviewbydream.evil.eda.Data.Data;
import com.findviewbydream.evil.eda.Data.UserData;
import com.findviewbydream.evil.eda.R;
import com.findviewbydream.evil.eda.Utils.Dialog.LoadingDialog;
import com.findviewbydream.evil.eda.Utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sunfusheng.marqueeview.MarqueeView;
import com.yuyh.easyadapter.recyclerview.EasyRVAdapter;
import com.yuyh.easyadapter.recyclerview.EasyRVHolder;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import cn.iwgang.familiarrecyclerview.FamiliarRecyclerView;
import okhttp3.Call;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Activity
 * 时间：16-9-24 上午10:17
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：流量兑换记录界面
 ********************************************/
public class FlowCallbackActivity extends BaseActivity
{
    private ImageView background;
    private FamiliarRecyclerView recyclerView;
    private MarqueeView marqueeView;
    private CardView null_sign;

    private Adapter adapter;
    private ArrayList<FlowCallbackBean> list;


    @Override
    public int setContentView()
    {
        return R.layout.flowcallback_activity;
    }

    @Override
    public String setToolbarTitle()
    {
        return "流量兑换记录";
    }

    @Override
    public void onCreate()
    {
        initView();

        initDate();

        setMarqueeView();

        LoadingDialog.showDialog(this);
        toNetWork();
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        background = (ImageView) findViewById(R.id.background);
        recyclerView = (FamiliarRecyclerView) findViewById(R.id.recyclerView);
        null_sign = (CardView) findViewById(R.id.null_sign);
        marqueeView = (MarqueeView) findViewById(R.id.marqueeView);
    }

    /**
     * 初始化数据
     */
    private void initDate()
    {
        Utils.Glide(R.drawable.login_background, background);
    }

    /**
     * 设置RecyclerView
     */
    private void setRecyclerView()
    {
        adapter = new Adapter(this, list, R.layout.flowcallback_item);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 设置顶部公告
     */
    private void setMarqueeView()
    {
        String notice = "    为了用户更好地使用“Eda易答”软件，" +
                "每月1日0：00至24日24：00正确答题所获得的流量于次日赠送，" +
                "25日0：00到月底答题流量于次月初赠送。如果您的流量尚未及时到账" +
                "，请您在反馈中提交申请，稍后会有工作人员进行处理。";
        marqueeView.startWithText(notice);
        marqueeView.setOnItemClickListener((position, textView) -> {
            startActivity(new Intent(this, FeedBackActivity.class));
        });
    }

    /**
     * 网络连接
     */
    private void toNetWork()
    {
        OkHttpUtils.post().url(Data.IP + "FlowCallbackServlet")
                .addParams("phone", UserData.getUserPhoneNum())
                .addParams("imei", UserData.getUserIMEI())
                .build()
                .execute(new StringCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
                        LoadingDialog.showError(FlowCallbackActivity.this, () -> toNetWork());
                    }

                    @Override
                    public void onResponse(String response, int id)
                    {
                        LoadingDialog.dismissDialog();

                        if (response.equals("unfinished"))
                        {
                            findViewById(R.id.unfinished_sign).setVisibility(View.VISIBLE);
                        }
                        else
                        {
                            list = new Gson().fromJson(response
                                    , new TypeToken<ArrayList<FlowCallbackBean>>()
                                    {}.getType());

                            if (list.size() == 0)
                                null_sign.setVisibility(View.VISIBLE);
                            else
                                setRecyclerView();
                        }
                    }
                });
    }


    private class Adapter extends EasyRVAdapter<FlowCallbackBean>
    {
        public Adapter(Context context, List<FlowCallbackBean> list, int... layoutIds)
        {
            super(context, list, layoutIds);
        }

        @Override
        protected void onBindData(EasyRVHolder viewHolder, int position, FlowCallbackBean item)
        {
            CardView cardView = viewHolder.getView(R.id.cardView);
            TextView datetime = viewHolder.getView(R.id.datetime);
            TextView textView = viewHolder.getView(R.id.textView);

            datetime.setText(item.getDatetime());

            String remarks = item.getRemarks().isEmpty() ? "无" : item.getRemarks();

            if (item.getState().equals("success"))
            {
                cardView.setCardBackgroundColor(Color.parseColor("#C0FFFFFF"));
                textView.setText("  ->赠送"
                        + item.getType() + "流量："
                        + item.getFlow() + ";              状态：成功"
                        + "\n  ->备注：\n                " + remarks);
            }
            else if (item.getState().equals("fail"))
            {
                cardView.setCardBackgroundColor(Color.parseColor("#80EE0000"));
                textView.setText("  ->赠送"
                        + item.getType() + "流量："
                        + item.getFlow() + ";              状态：失败"
                        + "\n  ->备注：\n                " + remarks);
            }

        }
    }


}
